import { onMounted, ref } from "vue";
import { getBcmUrl, bcmRequest } from "./bcmServices";

export function useBcmConfig(paramNameList = ['ENABLE_AUTO_OPEN_BREAKAGE_MODAL']) {
    const bcmConfig = ref<any[]>([]);
    const loaded = ref(false)
    const bcmUrl = getBcmUrl()

    const loadBcmConfig = async function (arr = []) {
        const ObjectData = {
            IsNotPage: true,
            extras: {
                isQueryDefault: true
            },
            WhereList: [{
                c_col: 'paramName',
                c_oper: '包含',
                c_value: arr && arr.length > 0 ? arr.join() : paramNameList.join(),
            }]
        }
        const response:any = await bcmRequest(bcmUrl.managerApi, '配置表',
            'EnjoyQueryList', ObjectData);
        bcmConfig.value = response?.ReturnObject;
    }

    const getBcmConfigValue = (name = 'ENABLE_AUTO_OPEN_BREAKAGE_MODAL') => {
        return !!name && bcmConfig.value?.find(v => v.paramName == name)?.paramValue || undefined
    }

    onMounted(() => {
        if (!loaded.value) {
            loaded.value = true;
            loadBcmConfig();
        }
    });

    return {
        getBcmConfigValue,
        bcmConfig,
        loadBcmConfig
    }

}